package com.nuonuo.util.tmp;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author duan
 * @version 1.0
 * @date 2023/7/31 13:25
 */
public class StreamSortTest {

    public static void main(String[] args) {
        SentinelInfo info1 = new SentinelInfo("3", 5);
        SentinelInfo info2 = new SentinelInfo("3", 7);
        SentinelInfo info3 = new SentinelInfo("2", 6);
        SentinelInfo info4 = new SentinelInfo("4", 8);

        List<SentinelInfo> infoList = Arrays.asList(info1, info2, info3, info4);

//        List<SentinelInfo> collect = infoList.stream().
//                sorted(Comparator.comparing((SentinelInfo v) -> StringUtils.isBlank(v.getServerMemory()) ? null : Integer.valueOf(v.getServerMemory()), Comparator.nullsFirst(Integer::compareTo)).reversed()
//                        .thenComparing(SentinelInfo::getUseCount, Comparator.nullsLast(Integer::compareTo)))
//                .collect(Collectors.toList());
//        List<SentinelInfo> collect = infoList.stream().sorted(
//                Comparator.comparing(SentinelInfo::getUseCount,
//                        Comparator.nullsFirst(Integer::compareTo)).reversed()).collect(Collectors.toList());

        List<SentinelInfo> collect = infoList.stream().sorted(
                Comparator.comparing((SentinelInfo v) -> StringUtils.isBlank(v.getServerMemory()) ?
                        null : Integer.valueOf(v.getServerMemory()), Comparator.nullsFirst(Integer::compareTo)).reversed()
                        .thenComparing(SentinelInfo::getUseCount, Comparator.nullsLast(Integer::compareTo)))
                .collect(Collectors.toList());
        for (SentinelInfo sentinelInfo : collect) {
            System.out.println(sentinelInfo.toString());
        }
    }

    public static class SentinelInfo {
        private String serverMemory;
        private int useCount;

        public SentinelInfo() {

        }

        public SentinelInfo(String serverMemory, int useCount) {
            this.serverMemory = serverMemory;
            this.useCount = useCount;
        }

        public String getServerMemory() {
            return serverMemory;
        }

        public void setServerMemory(String serverMemory) {
            this.serverMemory = serverMemory;
        }

        public int getUseCount() {
            return useCount;
        }

        public void setUseCount(int useCount) {
            this.useCount = useCount;
        }

        @Override
        public String toString() {
            return "SentinelInfo{" +
                    "serverMemory=" + serverMemory +
                    ", useCount=" + useCount +
                    '}';
        }
    }
}
